3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
26 class ApiUndelete
extends ApiBase
{
28 public function execute() {
29 $this->useTransactionalTimeLimit();
31 $params = $this->extractRequestParams();
33 $user = $this->getUser();
34 if ( $user->isBlocked() ) {
35 $this->dieBlocked( $user->getBlock() );
38 $titleObj = Title
::newFromText( $params['title'] );
39 if ( !$titleObj ||
$titleObj->isExternal() ) {
40 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
43 if ( !$titleObj->userCan( 'undelete', $user, 'secure' ) ) {
44 $this->dieWithError( 'permdenied-undelete' );
47 // Check if user can add tags
48 if ( !is_null( $params['tags'] ) ) {
49 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $user );
50 if ( !$ableToTag->isOK() ) {
51 $this->dieStatus( $ableToTag );
56 if ( !isset( $params['timestamps'] ) ) {
57 $params['timestamps'] = [];
59 if ( !is_array( $params['timestamps'] ) ) {
60 $params['timestamps'] = [ $params['timestamps'] ];
62 foreach ( $params['timestamps'] as $i => $ts ) {
63 $params['timestamps'][$i] = wfTimestamp( TS_MW
, $ts );
66 $pa = new PageArchive( $titleObj, $this->getConfig() );
67 $retval = $pa->undelete(
68 ( $params['timestamps'] ??
[] ),
75 if ( !is_array( $retval ) ) {
76 $this->dieWithError( 'apierror-cantundelete' );
80 Hooks
::run( 'FileUndeleteComplete',
81 [ $titleObj, $params['fileids'], $this->getUser(), $params['reason'] ] );
84 $this->setWatch( $params['watchlist'], $titleObj );
86 $info['title'] = $titleObj->getPrefixedText();
87 $info['revisions'] = intval( $retval[0] );
88 $info['fileversions'] = intval( $retval[1] );
89 $info['reason'] = $retval[2];
90 $this->getResult()->addValue( null, $this->getModuleName(), $info );
93 public function mustBePosted() {
97 public function isWriteMode() {
101 public function getAllowedParams() {
104 ApiBase
::PARAM_TYPE
=> 'string',
105 ApiBase
::PARAM_REQUIRED
=> true
109 ApiBase
::PARAM_TYPE
=> 'tags',
110 ApiBase
::PARAM_ISMULTI
=> true,
113 ApiBase
::PARAM_TYPE
=> 'timestamp',
114 ApiBase
::PARAM_ISMULTI
=> true,
117 ApiBase
::PARAM_TYPE
=> 'integer',
118 ApiBase
::PARAM_ISMULTI
=> true,
121 ApiBase
::PARAM_DFLT
=> 'preferences',
122 ApiBase
::PARAM_TYPE
=> [
132 public function needsToken() {
136 protected function getExamplesMessages() {
138 'action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page'
139 => 'apihelp-undelete-example-page',
140 'action=undelete&title=Main%20Page&token=123ABC' .
141 '×tamps=2007-07-03T22:00:45Z|2007-07-02T19:48:56Z'
142 => 'apihelp-undelete-example-revisions',
146 public function getHelpUrls() {
147 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Undelete';